local cjson = require "cjson"
local delay = 20
-- 导入common函数库


function put_log_into_mysql()
    local mysql = require("lib.mysql")
    local db = mysql:new()



-- get data from shared dict and put them into mysql

     local key = "logs"
     local vals = ""
     local times=0;
     local temp_val = ngx.shared.logs:lpop(key)
        ngx.log(ngx.ERR,"开始遍历")
    while (temp_val ~= nil)
    do
     --   ngx.log(ngx.ERR,"==============ini=="..temp_val)
     --   local redis = require "lib.redis"  --引入redis模块
     --   local red = redis:new()
     --
     --   red:lpush(key, temp_val)
     --   redis:close_redis(red)

        local json = cjson.decode(temp_val)
        local currentTime = "\"" .. json['currentTime'] .. "\""

        local req_body = '-'

        if json['req_body']  then
            req_body = json['req_body']
            req_body = string.gsub(req_body,"\n","")
            req_body = string.gsub(req_body,"\t","")
            req_body = string.gsub(req_body,"\"","'")
        end

        req_body = "\"" .. req_body .. "\""

        local req_status = json['status'] or 0



        local req_time =  json['request_time'] or 0
        local upstream_response_time=      json['upstream_response_time'] or -100

        local req_req = "\"" ..json['req_req']  .. "\""
        --local remote_addr = "\"" .. ngx.var.http_x_forwarded_for .. "\""
        local remote_addr = "\"" ..(json['remote_addr'] or '') .. "\""

        local  upstream_addr = json['upstream_addr'] or ''

        local  request_time = json['request_time'] or -100
        local server_addr = "\"" ..upstream_addr   .. "\""

         local myparams = ("("..remote_addr..",".. server_addr..","..currentTime..","..request_time.. ","..  upstream_response_time ..","..json['body_bytes_sent']  ..","..json['status'] ..","..req_req..","..req_body..")")

         vals = vals .. ",".. myparams
         temp_val = ngx.shared.logs:lpop(key)
         times=times+1
         if times>6000 then
             break;
         end

        end

     if vals ~= "" then
          vals = string.sub(vals, 2,-1)
          local command = ("insert into log_record (access_ip,server_ip,access_time,run_time,es_response_time,request_body_byte,run_state,url,post_data) values "..vals)


          local res, err, errcode, sqlstate = db:query(command)
         ngx.log(ngx.ERR,"insert into log_record")
          if not res then
              ngx.log(ngx.ERR,"command is ",command)
               ngx.log(ngx.ERR,"insert error: ", err, ": ", errcode, ": ", sqlstate, ".")
               return
          end
         vals =“”；
     end

  --  ok, err = db:set_keepalive(60000, 10)
     local ok, err = db:close()
     if not ok then
          ngx.log(ngx.ERR,"failed to close: ", err)
          return
     end

end
function crontab(premature)

-- decycle call timer to run put_log_into_mysql method, just like crontab
    if ngx.worker.id() == 0 then
        pull_mysql_into_nginx()
        pull_redis_into_nginx()
      --  put_log_into_mysql()
        local ok, err = ngx.timer.at(delay, crontab);
        --if not ok then
        --    ngx.log(ngx.ERR, "failed to create timer: ", err)
        --    return
        --end

    end
end

local ok, err = ngx.timer.at(delay, crontab)

--if not ok then
--     ngx.log(ngx.ERR, "failed to create timer: ", err)
--return
--
--end


function pull_redis_into_nginx()
    local redis = require "lib.redis"  --引入redis模块
    local red = redis:new()
    local key = "limit:ip:blacklist"

    local shared_ip_blacklist = ngx.shared.shared_ip_blacklist
    local redis = require "lib.redis"  --引入redis模块
    local red = redis:new()

   -- ngx.log(ngx.ERR, "从redis:获得数据，pull_redis_into_nginx ")
    if not red  then
        redis:close_redis(red)
        ngx.log(ngx.ERR, "limit ip cannot connect redis")
        local reponseJson =  ' {\"code\":901,\"data\":null,\"detail\":\"\"连接redis失败,\"msg\":\"\"} '
        ngx.say(reponseJson)
        ngx.exit(ngx.status)

        --  errlog("limit ip cannot connect redis"..err);
    else
        local ip_blacklist, err = red:smembers(key);

        if err then
            ngx.log(ngx.ERR, "limit ip smembers")
            local reponseJson =  ' {\"code\":901,\"data\":null,\"detail\":\"\"limit ip smembers失败,\"msg\":\"\"} '
            ngx.say(reponseJson)
            ngx.exit(ngx.status)
            --
            ----  errlog("limit ip smembers"..err);
        else
            ----刷新本地缓存，重新设置
            shared_ip_blacklist:flush_all();
            --
            ----同步redis黑名单 到 本地缓存
            for i,bip in ipairs(ip_blacklist) do
                --本地缓存redis中的黑名单
                ngx.log(ngx.ERR, "limit:ip:blacklist "..bip)
                shared_ip_blacklist:set(bip,true);
            end
            ----设置本地缓存的最新更新时间
            shared_ip_blacklist:set("last_update_time",ngx.now());
        end
    end
end

function pull_mysql_into_nginx()
    local mysql = require("lib.mysql")
    local db = mysql:new()
    -- get data from shared dict and put them into mysql

    local key = "cache_list"
    local vals = ""
    local expire_time=1200;
    local cache_list = ngx.shared.cache_list
    if db ==nil then
        ngx.log(ngx.ERR, "从MySQL:获得数据失败，pull_mysql_into_nginx ")
        return
    end
    cache_list:flush_all()
--    ngx.log(ngx.ERR, "从MySQL:获得数据，pull_mysql_into_nginx ")


    local res, err, errno, sqlstate = db:query('select * from setting_URL ')
    db:close()

    for i, row in ipairs(res) do
        --url=row["url"]
        --is_limit=row["is_limit"]
        --limit_max=row["limit_max"]
        --is_cache=row["is_cache"]
        --cache_time=row.cache_time
        cache_list:delete(row["url"])
        cache_list:set(row["url"], cjson.encode(row))
        ngx.log(ngx.ERR,"cache_list:get ",cache_list:get(row["url"]))
    end


 --   ngx.log(ngx.ERR,"select * from setting_URL")
    if not res then
       ngx.log(ngx.ERR,"command is ",command)
       ngx.log(ngx.ERR,"select * from setting_URL error: ", err, ": ", errcode, ": ", sqlstate, ".")
       return
    end


    --  ok, err = db:set_keepalive(60000, 10)
    local ok, err = db:close()
    --if not ok then
    --    ngx.log(ngx.ERR,"failed to close: ", err)
    --    return
    --end

    -- decycle call timer to run put_log_into_mysql method, just like crontab

    --local ok, err = ngx.timer.at(delay, pull_into_nginx);
    --if not ok then
    --    ngx.log(ngx.ERR, "failed to create timer: ", err)
    --    return
    --end
end
--ok, err = ngx.timer.at(delay, pull_into_nginx)
--if not ok then
--    ngx.log(ngx.ERR, "failed to create timer: ", err)
--    return
--
--end